﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                MarkiList.Items.Clear();

                ListItem element1 = new ListItem();
                element1.Text = "Производители";
                element1.Value = "";
                MarkiList.Items.Add(element1);

                MySqlConnection konekcija = new MySqlConnection();
                konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
               // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

                MySqlCommand komanda = new MySqlCommand();
                komanda.Connection = konekcija;
                komanda.CommandText = "SELECT * FROM proizvoditeli";

                try
                {
                    konekcija.Open();

                    MySqlDataReader citac = komanda.ExecuteReader();
                    while (citac.Read())
                    {
                        ListItem element = new ListItem();
                        element.Text = citac["ImeProizvoditel"].ToString();
                        element.Value = citac["Broj"].ToString();
                        MarkiList.Items.Add(element);
                    }
                    citac.Close();
                }
                catch (Exception ex)
                {
                   // Label2.Text = ex.Message;
                }
                finally
                {
                    konekcija.Close();
                }

                LinkButton1.Visible = false;
                SlikaPocetna.ImageUrl = "Sliki/RCZ-Design[320x240].jpg";
                ProizvoditelSlika.ImageUrl = "Sliki/Logo2.png";
            }

        }

        protected void PrikaziModeli(string Marka)
        {
            ModeliList.Items.Clear();

            ListItem element1 = new ListItem();
            element1.Text = "Модели";
            element1.Value = "";
            ModeliList.Items.Add(element1);

            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT * FROM modeli WHERE Proizvoditel='" + Marka + "'";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["Model"].ToString();
                    element.Value = citac["Model"].ToString();
                    ModeliList.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
             //   Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }

        }

        protected void MarkiList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (MarkiList.SelectedIndex == 0)
            {
                ModeliList.Items.Clear();
                LinkButton1.Visible = false;
                ModelLabel.Text = MarkaLabel.Text = ModelLabel1.Text = TipLabel.Text = ModelHyperLink.Text = ModelHyperLink.NavigateUrl = ModelHyperLink.ToolTip = ModelHyperLink.Target = LinkButton1.ToolTip = "";
                ProizvoditelSlika.ImageUrl = "Sliki/Logo2.png";
                SlikaPocetna.ImageUrl = "Sliki/RCZ-Design[320x240].jpg";
            }
            else
            {
                Proizvoditel.Text = MarkiList.SelectedItem.Text;
                PrikaziModeli(Proizvoditel.Text);
                ModelLabel.Text = MarkaLabel.Text = ModelLabel1.Text = TipLabel.Text = ModelHyperLink.Text = ModelHyperLink.NavigateUrl = ModelHyperLink.ToolTip = ModelHyperLink.Target = LinkButton1.ToolTip = "";
                LinkButton1.Visible = false;
                ProizvoditelSlika.ImageUrl = "Sliki/" + Proizvoditel.Text + "_Logo.png";
                SlikaPocetna.ImageUrl = "Sliki/RCZ-Design[320x240].jpg";
            }
        }

        protected void ModeliList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ModeliList.SelectedIndex != 0)
            {
                Model.Text = ModeliList.SelectedItem.Value;
                ModelLabel.Text = Proizvoditel.Text + " " + Model.Text;
                LoadInfoModel(ModeliList.SelectedValue);
                LinkButton1.Visible = true;
            }
            else
            {
                SlikaPocetna.ImageUrl = "Sliki/RCZ-Design[320x240].jpg";
                ModelLabel.Text = MarkaLabel.Text = ModelLabel1.Text = TipLabel.Text = ModelHyperLink.Text = ModelHyperLink.NavigateUrl = ModelHyperLink.ToolTip = ModelHyperLink.Target = LinkButton1.ToolTip = "";
                LinkButton1.Visible = false;
            }
        }

        protected void LoadInfoModel(string ID)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT * FROM modeli M, proizvoditeli P WHERE M.Model='" + ID + "' AND M.Proizvoditel=P.ImeProizvoditel";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                if (citac.Read())
                {
                    MarkaLabel.Text = citac["Proizvoditel"].ToString();
                    ModelLabel1.Text = citac["Model"].ToString();
                    TipLabel.Text = citac["Tip"].ToString();

                    ModelHyperLink.Text = citac["WebProizvoditeli"].ToString();
                    ModelHyperLink.NavigateUrl = citac["WebModeli"].ToString();
                    ModelHyperLink.ToolTip = "Официјална web страна за " + Proizvoditel.Text + " " + Model.Text;
                    ModelHyperLink.Target = "_blank";
                    LinkButton1.ToolTip = "Повеќе информации за " + Proizvoditel.Text + " " + Model.Text;
                    SlikaPocetna.ImageUrl = "Sliki/" + MarkaLabel.Text + "/320x240/" + citac["Slika"].ToString();
                    citac.Close();
                }
            }
            catch (Exception ex)
            {
                // Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Informacii.aspx?Marka=" + Proizvoditel.Text + "&Model=" + Model.Text);
        }
    }
}
